import runSaga from "./runSaga";
import stdChannel from "./channel";

function createSagaMiddleware() {
  const channel = stdChannel();
  let boundRunSaga;
  function sagaMiddleWare({ getState, dispatch }) {
    boundRunSaga = runSaga.bind(null, { channel, dispatch, getState });
    return function (next) {
      return function (action) {
        const result = next(action);
        channel.trigger(action);
        return result;
      };
    };
  }
  sagaMiddleWare.run = (saga) => boundRunSaga(saga);
  return sagaMiddleWare;
}
export default createSagaMiddleware;
